package com.example.mywiseelves.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.mywiseelves.R;
import com.example.mywiseelves.adapter.FragmentAdapter;
import com.example.mywiseelves.bean.FragmentBean;
import com.example.mywiseelves.dao.ElvesDao;
import com.example.mywiseelves.dao.ResourcesDao;
import com.example.mywiseelves.dao.UserDao;
import com.example.mywiseelves.dao.VoicesDao;
import com.example.mywiseelves.fragment.ExitFragment;
import com.example.mywiseelves.fragment.HelpFragment;
import com.example.mywiseelves.fragment.HomeFragment;
import com.example.mywiseelves.fragment.IllustrateFragment;
import com.example.mywiseelves.fragment.WisdomFragment;
import com.example.mywiseelves.service.ElvesViewService;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.BezierPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;

/**
 * 创建5基本个碎片：起源树中心，帮助中心，图鉴中心，智慧中心，退出中心，并使用ViewPager和MagicIndicator修饰。
 */
public class HomeActivity extends AppCompatActivity {
    MagicIndicator magicIndicator;//指示器
    ViewPager viewPager;//可供左右滑动的视图控件
    Context context;//上下文
    ArrayList<FragmentBean> fragmentList = new ArrayList<>();
    FragmentAdapter fragmentAdapter;//碎片适配器


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.context = this;
        //添加fragment进ViewPager
        initViewPager();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Intent intent = new Intent(getApplicationContext(), ElvesViewService.class);
        stopService(intent);
    }

    /**
     * 使用ViewPager控制fragment
     */
    private void initViewPager() {
        //初始化Fragment数据
        initFragment();
        //实例化viewPager对象
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        //实例化碎片适配器对象
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), 0, fragmentList);
        viewPager.setAdapter(fragmentAdapter);
        //实例化指示器控件
        magicIndicator = findViewById(R.id.magic_indicator);

        //初始化指示器
        initMagicIndicator();
    }

    /**
     * 初始化Fragment数据
     */
    public void initFragment() {
        fragmentList.add(new FragmentBean("联结之书", IllustrateFragment.newInstance("", "")));
        fragmentList.add(new FragmentBean("永恒石刻", HelpFragment.newInstance("", "")));
        fragmentList.add(new FragmentBean("祈愿之树", HomeFragment.newInstance("", "")));
        fragmentList.add(new FragmentBean("贤者之石", WisdomFragment.newInstance("", "")));
        fragmentList.add(new FragmentBean("传送水晶", ExitFragment.newInstance("", "")));
    }


    /**
     * 给MagicIndicator设置指示器
     */
    private void initMagicIndicator() {
        //ViewPager指示器
        CommonNavigator commonNavigator = new CommonNavigator(this);
        //调整标题栏
        commonNavigator.setAdjustMode(true);
        //给指示器添设适配器,用于切换指示器的样式，目前内建了好几种指示器
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            /**特殊点的*/
            @Override
            public int getCount() {
                return fragmentList.size();
            }

            //重写CommonNavigatorAdapter的抽象方法，获得指示器标题的样式
            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {

                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(HomeActivity.this);
                commonPagerTitleView.setContentView(R.layout.magic_title);

                // 初始化
                final TextView titleText = commonPagerTitleView.findViewById(R.id.title_text);
                titleText.setText(fragmentList.get(index).getTitle());

                //添加点击事件
                commonPagerTitleView.setOnClickListener(v -> viewPager.setCurrentItem(index));

                commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {


                    @Override
                    public void onSelected(int index, int totalCount) {

                    }

                    @Override
                    public void onDeselected(int index, int totalCount) {

                    }

                    @Override
                    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
                        titleText.setTextSize(10);


                    }

                    @Override
                    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
                        titleText.setTextSize(18);

                    }
                });
                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                BezierPagerIndicator indicator = new BezierPagerIndicator(context);
                indicator.setColors(Color.parseColor("#FFDD00"));
                return indicator;
            }

        });

        //添加指示器到控件里
        magicIndicator.setNavigator(commonNavigator);
        magicIndicator.onPageSelected(2);
        viewPager.setCurrentItem(2);
        LinearLayout titleContainer = commonNavigator.getTitleContainer();
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return UIUtil.dip2px(context, 0);
            }
        });
        //绑定指示器和左右滑动视图控件
        ViewPagerHelper.bind(magicIndicator, viewPager);
    }
}